﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using DevExpress.XtraReports.UI;
namespace EntryExit
{
    public class ReportManager
    {
        public ISqlDBManager DbManager { get; set; }
        private DataTable resultTable { get; set; }
        private DataSet resultDs { get; set; }
        public ReportManager()
        {
            DbManager = new SqlDBManager();
        }
        public XtraReport rptDashBoard(List<Employee> selectedEmployees, DateTime StartDate, DateTime EndDate)
        {
            XtraReport report = new XtraReport();
            string selectedLPNs = "";
            foreach (var item in selectedEmployees)
            {
                selectedLPNs += item.LPN + "|";
            }
            if (selectedLPNs.Length > 0)

                selectedLPNs = selectedLPNs.Remove(selectedLPNs.Length - 1);
             
            if (!string.IsNullOrEmpty(selectedLPNs) && selectedLPNs.Length > 0)
            {
                SqlParameter[] sqlParams = new SqlParameter[] { new SqlParameter() { ParameterName = "@userLPNs", SqlDbType = SqlDbType.VarChar, Value = selectedLPNs }, 
                new SqlParameter() { ParameterName = "@START_DATE", SqlDbType = SqlDbType.VarChar, Value = Common.FormatDate(StartDate) },
            new SqlParameter() { ParameterName = "@END_DATE", SqlDbType = SqlDbType.VarChar, Value = Common.FormatDate(EndDate) }};
                //SqlParameter[] sqlParams = new SqlParameter[] { };
                //SqlParameter[] sqlParams = new SqlParameter[] {  };
                resultDs = DbManager.GetData(DBConstants.SP_AMS_Rpt_Dashboard, CommandType.StoredProcedure, DBName.SERVICE, sqlParams, "DS");

                if (resultDs.Tables[0].Rows.Count > 0)
                    report = new DashboardReport(resultDs.Tables[0], resultDs.Tables[1], resultDs.Tables[2], resultDs.Tables[3]);
                //DataTable dtEmployees = ConvertToDataTable<Employee>(selectedEmployees);


            }
            return report;
        }

        public DataTable ConvertToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
               TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;

        }

    }
}